/*
 * 正则表达式demo
 */
#include <regex>
#include <iostream>
#include <string>
#include "../log/log.h"
using namespace std;

int getString(const string& inStr, vector<string>& outStr, const string rule)
{
    LOG("Funtion start: getString.————%s", rule.c_str());
    regex r(rule);
    smatch results;
    
    // LOG("start to loop A.");
    // while(regex_search(b, e, results, r)) {// 只会返回0或1
        // LOG("%s",results.str().c_str());
        // b = results[0].second;
    // }
    
    // LOG("start to loop B.");
    // for (sregex_iterator it(str.begin(), str.end(), r), end_it; it != end_it; ++it) {
        // LOG("%s",it->str().c_str());
    // }
    
    // LOG("start to loop B.");
    for (sregex_iterator it(inStr.begin(), inStr.end(), r), end_it; it != end_it; ++it) {
        LOG("%s",it->str().c_str());
        outStr.emplace_back(it->str().c_str());
    }
    
    return outStr.size();
}

int main()
{
    string str("<testsuites name=\"summary_report\" starttime=\"2022-12-05 09:18:33\" endtime=\"2022-12-05 09:53:54\" errors=\"0\" disabled=\"0\" failures=\"5\" tests=\"335\" ignored=\"0\" unavailable=\"0\" productinfo=\"{}\" modules=\"1\" runmodules=\"1\">");
    
    LOG("init regex rule.");
    // string re_failures("(?<=endtime=\").{19}(?=\")"); // 不能使用?=这样的非捕获匹配语法
    string re_name("name=\".*?\"");
    string re_starttime("starttime=\"(\\d|-| |:)*\"");
    string re_endtime("endtime=\"(\\d|-| |:)*\"");
    string re_errors("errors=\"\\d*\"");
    string re_disabled("disabled=\"\\d*\"");
    string re_failures("failures=\"\\d*\"");
    string re_tests("tests=\"\\d*\"");
    string re_ignored("ignored=\"\\d*\"");
    string re_unavailable("unavailable=\"\\d*\"");
    vector<string> reVec{re_name, re_starttime, re_endtime, re_errors, re_disabled, re_failures, re_tests, re_ignored, re_unavailable};

    LOG("start to getString.");
    vector<string> getStrs;
    for ( auto re : reVec ) {
        getString(str, getStrs, re);
    }
    
    return 0;
}

// g++ 